#include "stdafx.h"
#include "BasicSkinBase.h"
#include "timer/BasicSkinTimer.h"
#include "menu/BasicSkinMenuObject.h"
#include "menu/BasicSkinMenuLoop.h"
#include <Windows.h>

namespace BasicSkinView
{
	using namespace BasicSkinMenu;
	using namespace BasicSkinTimer;

	LRESULT WINAPI BasicSkinBaseWndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);

	WNDPROC WINAPI Sb_GetWndProc()
	{
		return BasicSkinBaseWndProc;
	}

	ATOM WINAPI Sb_RegisterClass( CONST WNDCLASS* lpWndClass )
	{
		return ::RegisterClass(lpWndClass);
	}

	HANDLE WINAPI Sb_CreateTimer( void* clsObj,void*clsFun )
	{
		CBasicSkinTimer* pTimer = new CBasicSkinTimer(clsObj,clsFun);
		return pTimer;
		//return NULL;
	}

	bool WINAPI Sb_StartTimer( HANDLE hTimer,int repeatIntervalMS )
	{
		CBasicSkinTimer* pTimer = (CBasicSkinTimer*)hTimer;
		///pTimer->start(repeatIntervalMS/1000.0,repeatIntervalMS/1000.0);
		pTimer->start(repeatIntervalMS,repeatIntervalMS);
		return true;
		//return false;
	}

	void WINAPI Sb_KillTimer( HANDLE hTimer )
	{
		CBasicSkinTimer* pTimer = (CBasicSkinTimer*)hTimer;
		pTimer->stop();
		delete pTimer;
	}

	struct stInterData 
	{
		LPVOID pObj;
		WNDPROC pFun;
	};
	HWND WINAPI Sb_CreateWindow( DWORD dwExStyle,LPCTSTR lpClassName,LPCTSTR lpWindowName,DWORD dwStyle, int x,int y,int nWidth,int nHeight,HWND hWndParent,HMENU hMenu,WNDPROC wndproc,LPVOID lParam )
	{
		stInterData sI;
		sI.pFun = wndproc;
		sI.pObj = lParam;
		return ::CreateWindowEx(dwExStyle,lpClassName,lpWindowName,dwStyle,x,y,nWidth,nHeight,hWndParent,hMenu,NULL,&sI);
	}

	LRESULT WINAPI BasicSkinBaseWndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
	{
		if(WM_NCCREATE == uMsg)
		{
			CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lParam);
			stInterData* pID = (stInterData*)cs->lpCreateParams;
			::SetWindowLongPtrW(hWnd,GWLP_WNDPROC,(LONG_PTR)pID->pFun);
			cs->lpCreateParams = pID->pObj;
			return pID->pFun(hWnd,uMsg,wParam,lParam);
		}
		return ::DefWindowProc(hWnd,uMsg,wParam,lParam);
	}

	/////////////////////////////////////////////////////////////////////////////////////////
	int WINAPI Sb_TrackPopupMenu( HMENUOBJ hMenu,UINT dwFlags,int x,int y,HWND hwndOwner )
	{
		return CBasicSkinMenuLoop::MyTrackPopupMenu((HSKINMENU)hMenu,dwFlags,x,y,hwndOwner);
	}

	HMENUOBJ WINAPI Sb_LoadMenu( HMODULE hMod,LPCWSTR lpMenuName )
	{
		return (HMENUOBJ)CBasicSkinMenuObject::LoadMenu(hMod,lpMenuName);
	}

	HMENUOBJ WINAPI Sb_GetSubMenu( HMENUOBJ hMenu,int nPos )
	{
		return CBasicSkinMenuObject::GetSubMenu((HSKINMENU)hMenu,nPos);
	}

	int WINAPI Sb_GetMenuString( HMENUOBJ hMenu,UINT uIDItem,LPTSTR lpString,int nMaxCount,UINT uFlag )
	{
		return 0;
	}

	HMENUOBJ WINAPI Sb_CreatePopupMenu()
	{
		return (HMENUOBJ)CBasicSkinMenuObject::SkinCreatePopupMenu();
	}

	bool WINAPI Sb_DestroyMenu( HMENUOBJ hMenu )
	{
		CBasicSkinMenuObject::SkinDestroyPopupMenu((HSKINMENU)hMenu);
		return true;
	}

	bool WINAPI Sb_InsertStringMenu( HMENUOBJ hMenu,UINT nPositoin,const wchar_t* pstr,UINT nID )
	{
		return CBasicSkinMenuObject::InsertStringMenu((HSKINMENU)hMenu,nPositoin,pstr,nID);
	}

	bool WINAPI Sb_EnableMenuItem( HMENUOBJ hMenu, UINT uIDEnableItem, UINT uEnable )
	{
		return CBasicSkinMenuObject::EnableMenuItem((HSKINMENU)hMenu,uIDEnableItem,uEnable);
	}

	bool WINAPI Sb_SetImageMenuItem( HMENUOBJ hMenu, UINT uIDEnableItem, Image* pImage )
	{
		return CBasicSkinMenuObject::SetImageMenuItem((HSKINMENU)hMenu,uIDEnableItem,pImage);
	}

	bool WINAPI Sb_CheckMenuItem( HMENUOBJ hMenu, UINT uIDEnableItem, UINT nCheck )
	{
		return CBasicSkinMenuObject::CheckMenuItem((HSKINMENU)hMenu,uIDEnableItem,nCheck);
	}

	bool WINAPI Sb_InsertSubPopUpMenu( HMENUOBJ hMenu,UINT nPositoin,const wchar_t* pstr,UINT nID )
	{
		return CBasicSkinMenuObject::InsertSubPopUpMenu((HSKINMENU)hMenu,nPositoin,pstr,nID);
	}
}